
package cn.jx.asc.login.controller;


import cn.jx.asc.common.untils.SecurityUtils;
import cn.jx.asc.user.pojo.User;
import cn.jx.asc.user.service.UserService;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;

/**
 * @author 周国伟
 * @desc TODO
 * @date 2018/5/31 9:21
 * @describe 登入控制
 */
@Controller("loginController")
@RequestMapping("/Login")
public class LoginController {

    @Autowired
    private UserService userService;

    @RequestMapping("login")
    public String Login(HttpServletRequest request, Model model){
        String username ="";
        String pwd = "";
        Cookie[] cookies = request.getCookies();
        if(cookies!=null){
            for (Cookie cookie:cookies){
                if("username".equals(cookie.getName())){
                    username = cookie.getValue();
                }else if("pwd".equals(cookie.getName())){
                    pwd = cookie.getValue();
                }
            }
        }
        model.addAttribute("username",username);
        model.addAttribute("pwd",pwd);

        return "login";
    }
    /**
     * @author ZhouGuoWei
     * @date 2018/6/1 15:00
     * @describe 注册用户
     */

    @RequestMapping("/sign")
    @ResponseBody
    public String toLogin(User user, HttpServletRequest request) throws UnsupportedEncodingException, NoSuchAlgorithmException {

        String username = request.getParameter("username");
        user.setUserName(request.getParameter("username"));
        user.setPassword(SecurityUtils.encryptPassword(request.getParameter("pwd")));
        user.setRealName(request.getParameter("realname"));

        User people = userService.findUserByname(username);
        if(people!=null){
            return "fail";
        }else {
            int rows = userService.addUser(user);
            if(rows>0){
                return "success";
            }else {
                return "fail";
            }
        }
    }

    /**
     * @author ZhouGuoWei
     * @date 2018/6/1 15:52
     * @describe 检查登入
     */
    @RequestMapping("/check")
    @ResponseBody
    public String checkLogin(HttpServletRequest request, HttpServletResponse response) throws UnsupportedEncodingException, NoSuchAlgorithmException {

        String username = request.getParameter("username");
        String pwd = request.getParameter("pwd");
        String flag =request.getParameter("checkbox");

        //先检查是否有这个用户
        User user = userService.findUserByname(username);
        if(user!=null){
            //检查密码是否相等
            if(SecurityUtils.checkPassword(pwd,user.getPassword())){
                //如果勾选了记住密码
                if("1".equals(flag)){
                    Cookie cookie_name = new Cookie("username",username);
                    Cookie cookie_pwd = new Cookie("pwd",pwd);
                    cookie_name.setMaxAge(60*60*24*3);                       //设置cookie的有效期为三天
                    cookie_pwd.setMaxAge(60*60*24*3);
                    cookie_name.setPath("/Login/login");                      //设置cookie的携带路径
                    cookie_pwd.setPath("/Login/login");
                    response.addCookie(cookie_name);                          //向客户端发送cookie
                    response.addCookie(cookie_pwd);
                    request.getSession().setAttribute("USER_SESSION",user);    //将用于存入SESSION中
                    //将正确的信息返回给前端
                    return "success";
                }else {
                    request.getSession().setAttribute("USER_SESSION",user);
                    //将正确的信息返回给前端
                    return "success";
                }

            }else {
                return "fail";
            }
        }else {
            return "fail";
        }

    }

    /**
     * @author ZhouGuoWei
     * @date 2018/6/8 21:43
     * @describe 退出登入
     */
    @RequestMapping("/loginOut")
    public String loginOut(HttpSession session){
        session.invalidate();//清除Session
        return "redirect:login"; //重定向到登入页面
    }

}
